package evan.curtis.beersnob;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import evan.curtis.beersnob.util.Constants;
import evan.curtis.beersnob.util.DatabaseFunctions;
import evan.curtis.beersnob.util.Vibration;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class FavoritesFragment extends Fragment {

	private View mContentView;
	private MainActivity mActivity;
	private LinearLayout resultsLayout;
	private JSONArray databaseValues;

	// ------------------------------------------------------------------------------------/

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mContentView = inflater.inflate(R.layout.favorites_layout, container,
				false);

		
		mActivity = (MainActivity) this.getActivity();
		
		resultsLayout = (LinearLayout) mContentView.findViewById(R.id.favoritesResultsLayout);

		new GetFavorites().execute(Constants.GET_FAVORITES);

		return mContentView;
	}

	private void updateTextViews() throws JSONException {
		Log.i("update", "updating text views");
		resultsLayout.removeAllViews();

		// check if there are no results
		if (databaseValues.length() == 0) {
			TextView t = new TextView(mActivity);
			t.setText("You have no favorites.");
			Log.i("json", "json array empty");
			resultsLayout.addView(t);
		}
		
		
		// if there are results, print them
		for (int i = 0; i < databaseValues.length(); i++) {
			JSONObject json_data = databaseValues.getJSONObject(i);

			final int id = json_data.getInt("id");
			final String name = json_data.getString("name");
			final String brewer = json_data.getString("brewer");
			final String style = json_data.getString("style");
			final int rating = json_data.getInt("rating");
			final String comment = json_data.getString("comment");
			

			// inflate layout to display results
			RelativeLayout rl = new RelativeLayout(mActivity);
			LayoutInflater inflater = (LayoutInflater) mActivity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			View volumeView = inflater.inflate(R.layout.card, rl);

			// initialize TextViews
			TextView nameText = (TextView) volumeView.findViewById(R.id.name);
			TextView brewerText = (TextView) volumeView.findViewById(R.id.brewer);
			TextView ratingText = (TextView) volumeView
					.findViewById(R.id.rating);

			// set Text
			nameText.setText(name);
			brewerText.setText(brewer);
			ratingText.setText(Integer.toString(rating));


			volumeView.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View arg0) {
					// change color of TextView and vibrate to show user that it
					// has been clicked

					Vibration.vibrate(20, mActivity);

					// create new Intent and pass values, then start activity
					Intent myIntent = new Intent(mActivity, BeerView.class);
					myIntent.putExtra("id", id);
					myIntent.putExtra("name", name);
					myIntent.putExtra("brewer", brewer);
					myIntent.putExtra("style", style);
					myIntent.putExtra("rating", rating);
					startActivity(myIntent);

					return false;
				}

			});

			resultsLayout.addView(volumeView);

		}
		Log.i("json", "finished");
	}

	private class GetFavorites extends AsyncTask<String, Void, String> {

		JSONArray returnValue = null;
		String filename;

		@Override
		protected String doInBackground(String... params) {

			filename = params[0];
			returnValue = DatabaseFunctions.getJSONArray(filename, "?name=" + Constants.USER_NAME);
			Log.i("username", Constants.USER_NAME);
			return null;

		}

		@Override
		protected void onPostExecute(String result) {
			databaseValues = returnValue;
			Log.i("size", Integer.toString(returnValue.length()));
			try {
				updateTextViews();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		@Override
		protected void onPreExecute() {

		}

		@Override
		protected void onProgressUpdate(Void... values) {

		}
	}

}
